package com.jmu.edu.IMApplication.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.ContactItemAdapter;
import com.jmu.edu.IMApplication.event.SwitchFragmentEvent;
import com.jmu.edu.IMApplication.utils.FriendUtils;
import com.jmu.edu.IMApplication.utils.ViewUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
import cn.jpush.im.android.api.model.UserInfo;


public class ContactFragment extends Fragment {

    private MaterialButton addButton, friendNotificationButton;
    private RecyclerView recyclerView;
    private ContactItemAdapter adapter;
    private List<UserInfo> friendList = new ArrayList<>();

    public ContactFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_contact, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

    }

    private void initView() {

        addButton = getView().findViewById(R.id.fragment_contact_add);

        friendNotificationButton = getView().findViewById(R.id.fragment_contact_friend_notification);
        initListener();
        recyclerView = getView().findViewById(R.id.fragment_contact_recyclerView);
        //设置为垂直排列
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        //初始化适配器并指定适配器给recyclerview
        adapter = new ContactItemAdapter();
        adapter.setUserInfoList(friendList);

        recyclerView.setAdapter(adapter);
        loadData();
    }

    private void initListener() {
        //添加好友的事件写在这里
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "searchFriend";
                Fragment show = MyApplication.fragmentManager.findFragmentByTag(tag);
                if (show == null) {
                    show = new SearchFriendFragment();
                }
                ViewUtils.showFragment(show, tag, R.anim.from_right, R.anim.out_left);
            }
        });
        //跳转到好友通知界面
        friendNotificationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "friendNotification";
                Fragment show = MyApplication.fragmentManager.findFragmentByTag(tag);
                if(show == null){
                    show = new FriendNotificationFragment();
                }
                ViewUtils.showFragment(show, tag, R.anim.from_right, R.anim.out_right);

            }
        });
    }

    public void loadData() {
        FriendUtils.getFriendList(new GetUserInfoListCallback() {
            @Override
            public void gotResult(int i, String s, List<UserInfo> list) {
                //加载数据集，并且告诉适配器数据集发生改变
                friendList = list;
                Log.d("好友列表", "当前好友列表数量:" + friendList.size());
                adapter.setUserInfoList(friendList);
                adapter.notifyDataSetChanged();
            }
        });
    }
}